package me.sdevil507.org.repository;

import me.sdevil507.org.dto.ExpertSearchDto;
import me.sdevil507.org.po.TempExpertPo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * Api用户repository
 *
 * @author sdevil507
 * created on 2019/12/15
 */
public interface TempExpertRepository extends JpaRepository<TempExpertPo, Long>, JpaSpecificationExecutor<TempExpertPo>, QuerydslPredicateExecutor<TempExpertPo> {

    /**
     * 根据手机号码查询用户信息
     *
     * @param phoneNumber 手机号码
     * @return 用户信息
     */
    TempExpertPo findByPhoneNumberAndDeletedIsFalse(String phoneNumber);

    /**
     * 根据姓名查询用户信息
     *
     * @param realName 姓名
     * @return 用户信息
     */
    List<TempExpertPo> findAllByRealNameAndOrgIDAndDeletedIsFalse(String realName, Long orgId);

    /**
     * 判断手机号码是否存在
     *
     * @param phoneNumber 手机号码
     * @return 执行反馈
     */
    boolean existsByPhoneNumberAndDeletedIsFalse(String phoneNumber);

    /**
     * 根据用户id列表查询出用户列表
     *
     * @param userIds 用户id列表
     * @return 用户列表
     */
    Set<TempExpertPo> findAllByIdIn(List<Long> userIds);

    /**
     * 根据手机号更改密码
     *
     * @param newPassword 新密码
     * @param phoneNumber 手机号
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update TempExpertPo a set a.password= :newPassword where a.phoneNumber= :phoneNumber and a.deleted=false ")
    void updatePasswordByPhoneNumber(@Param("newPassword") String newPassword, @Param("phoneNumber") String phoneNumber);

    /**
     * 重置密码
     *
     * @param id          用户id
     * @param newPassword 密码
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update TempExpertPo a set a.password= :newPassword where a.id= :id")
    void resetPassword(@Param("id") Long id, @Param("newPassword") String newPassword);

    /**
     * 逻辑删除
     *
     * @param id 用户id
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update TempExpertPo a set a.deleted=true where a.id=:id")
    void logicDelete(@Param("id") Long id);

    @Query(nativeQuery = true, value = "SELECT\n" +
            "       a.isTmep,\n" +
            "		a.author,\n" +
            "		a.orgUserId,\n" +
            "		a.phoneNumber \n" +
            "FROM\n" +
            "	(\n" +
            "	SELECT FALSE AS\n" +
            "		isTmep,\n" +
            "		bou.real_name AS author,\n" +
            "		bou.id AS orgUserId,\n" +
            "		bou.phone_number AS phoneNumber \n" +
            "	FROM\n" +
            "		biz_org_user_rel bour\n" +
            "		LEFT JOIN user bou ON bour.user_id = bou.id \n" +
            "	WHERE\n" +
            "		bour.expert = 1 \n" +
            "		AND IF(ISNULL(?1), true, bour.org_id =?1 ) \n" +
            "		AND bou.deleted = 0 " +
            "UNION\n" +
            "	SELECT TRUE AS\n" +
            "		isTmep,\n" +
            "		bte.real_name AS author,\n" +
            "		bte.id AS orgUserId,\n" +
            "		bte.phone_number AS phoneNumber \n" +
            "	FROM\n" +
            "		biz_temp_expert bte \n" +
            "	WHERE\n" +
            "		IF(ISNULL(?1), true, bte.orgid =?1 ) \n" +
            "		AND bte.deleted = 0 \n" +
            "	) a \n" +
            "ORDER BY\n" +
            "	a.author")
    Set<ExpertSearchDto> getAllExpertData(Long orgId);
}
